/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.UserSession;
import java.awt.MouseInfo;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import view.insert.*;
import view.manage.ViewManageCliente;

/**
 *
 * @author Flash
 */
public class FramePrincipal extends javax.swing.JFrame {

    /**
     * Creates new form Principal
     */
    private static FramePrincipal sFramePrincipal = null;
    
    private FramePrincipal() {
        initComponents();
        ViewUtil.centralizaComponente(this);
    }
    
    public static FramePrincipal getInstance() {
        return (sFramePrincipal == null) ? sFramePrincipal = new FramePrincipal() : sFramePrincipal;
    }
    


    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupCliente = new javax.swing.JPopupMenu();
        menuClienteNovo = new javax.swing.JMenuItem();
        menuClienteGerenciar = new javax.swing.JMenuItem();
        popupFornecedor = new javax.swing.JPopupMenu();
        menuFornecedorNovo = new javax.swing.JMenuItem();
        menuFornecedorGerenciar = new javax.swing.JMenuItem();
        popupFuncionario = new javax.swing.JPopupMenu();
        menuFuncionarioNovo = new javax.swing.JMenuItem();
        menuFuncionarioGerenciar = new javax.swing.JMenuItem();
        popupVenda = new javax.swing.JPopupMenu();
        menuVendaNovo = new javax.swing.JMenuItem();
        menuVendaGerenciar = new javax.swing.JMenuItem();
        popupCompra = new javax.swing.JPopupMenu();
        menuCompraNovo = new javax.swing.JMenuItem();
        menuCompraGerenciar = new javax.swing.JMenuItem();
        popupColheita = new javax.swing.JPopupMenu();
        menuColheitaNovo = new javax.swing.JMenuItem();
        menuColheitaGerenciar = new javax.swing.JMenuItem();
        popupUsuario = new javax.swing.JPopupMenu();
        menuUsuarioNovo = new javax.swing.JMenuItem();
        menuUsuarioGerenciar = new javax.swing.JMenuItem();
        btnVenda = new javax.swing.JButton();
        btnFornecedor = new javax.swing.JButton();
        btnFuncionario = new javax.swing.JButton();
        btnCliente = new javax.swing.JButton();
        btnCompra = new javax.swing.JButton();
        btnColheita = new javax.swing.JButton();
        btnRelatorio = new javax.swing.JButton();
        btnUsuario = new javax.swing.JButton();
        btnSair = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();

        menuClienteNovo.setText("Novo");
        menuClienteNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuClienteNovoActionPerformed(evt);
            }
        });
        popupCliente.add(menuClienteNovo);

        menuClienteGerenciar.setText("Gerenciar");
        menuClienteGerenciar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuClienteGerenciarActionPerformed(evt);
            }
        });
        popupCliente.add(menuClienteGerenciar);

        menuFornecedorNovo.setText("Novo");
        menuFornecedorNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFornecedorNovoActionPerformed(evt);
            }
        });
        popupFornecedor.add(menuFornecedorNovo);

        menuFornecedorGerenciar.setText("Gerenciar");
        popupFornecedor.add(menuFornecedorGerenciar);

        menuFuncionarioNovo.setText("Novo");
        menuFuncionarioNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFuncionarioNovoActionPerformed(evt);
            }
        });
        popupFuncionario.add(menuFuncionarioNovo);

        menuFuncionarioGerenciar.setText("Gerenciar");
        popupFuncionario.add(menuFuncionarioGerenciar);

        menuVendaNovo.setText("Novo");
        menuVendaNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuVendaNovoActionPerformed(evt);
            }
        });
        popupVenda.add(menuVendaNovo);

        menuVendaGerenciar.setText("Gerenciar");
        popupVenda.add(menuVendaGerenciar);

        menuCompraNovo.setText("Novo");
        menuCompraNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuCompraNovoActionPerformed(evt);
            }
        });
        popupCompra.add(menuCompraNovo);

        menuCompraGerenciar.setText("Gerenciar");
        popupCompra.add(menuCompraGerenciar);

        menuColheitaNovo.setText("Novo");
        menuColheitaNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuColheitaNovoActionPerformed(evt);
            }
        });
        popupColheita.add(menuColheitaNovo);

        menuColheitaGerenciar.setText("Gerenciar");
        popupColheita.add(menuColheitaGerenciar);

        menuUsuarioNovo.setText("Novo");
        menuUsuarioNovo.setToolTipText("");
        menuUsuarioNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuUsuarioNovoActionPerformed(evt);
            }
        });
        popupUsuario.add(menuUsuarioNovo);

        menuUsuarioGerenciar.setText("Gerenciar");
        popupUsuario.add(menuUsuarioGerenciar);

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Lichias da Serrinha");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        btnVenda.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Venda.png"))); // NOI18N
        btnVenda.setText("Venda");
        btnVenda.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVendaActionPerformed(evt);
            }
        });

        btnFornecedor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Fornecedor.png"))); // NOI18N
        btnFornecedor.setText("Fornecedor");
        btnFornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFornecedorActionPerformed(evt);
            }
        });

        btnFuncionario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Funcionario.png"))); // NOI18N
        btnFuncionario.setText("Funcionário");
        btnFuncionario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnFuncionarioActionPerformed(evt);
            }
        });

        btnCliente.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Cliente.png"))); // NOI18N
        btnCliente.setText("Cliente");
        btnCliente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnClienteActionPerformed(evt);
            }
        });

        btnCompra.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Compra.png"))); // NOI18N
        btnCompra.setText("Compra");
        btnCompra.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCompraActionPerformed(evt);
            }
        });

        btnColheita.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Colheita.png"))); // NOI18N
        btnColheita.setText("Colheita");
        btnColheita.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnColheitaActionPerformed(evt);
            }
        });

        btnRelatorio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Relatorio.png"))); // NOI18N
        btnRelatorio.setText("Relatório");

        btnUsuario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Usuario.png"))); // NOI18N
        btnUsuario.setText("Usuário");
        btnUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUsuarioActionPerformed(evt);
            }
        });

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Sair.png"))); // NOI18N
        btnSair.setText("Sair");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        jMenu1.setText("Ajuda");
        jMenuBar1.add(jMenu1);

        jMenu2.setText("Sobre");
        jMenu2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu2MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu2);

        jMenu3.setText("Efetuar Logoff");
        jMenu3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu3MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu3);

        jMenu4.setText("Sair");
        jMenu4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenu4MouseClicked(evt);
            }
        });
        jMenuBar1.add(jMenu4);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnCliente, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnFornecedor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnFuncionario))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(btnVenda, javax.swing.GroupLayout.DEFAULT_SIZE, 157, Short.MAX_VALUE)
                    .addComponent(btnCompra, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnColheita, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnRelatorio, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSair, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 157, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnCliente, btnFornecedor, btnVenda});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnVenda)
                        .addComponent(btnUsuario))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnCliente)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnFornecedor)
                            .addComponent(btnCompra)
                            .addComponent(btnRelatorio))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(btnFuncionario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnColheita, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(btnSair, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnCliente, btnFornecedor, btnVenda});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnFuncionarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFuncionarioActionPerformed
        showMenu(popupFuncionario);
    }//GEN-LAST:event_btnFuncionarioActionPerformed

    private void btnClienteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnClienteActionPerformed
        showMenu(popupCliente);
    }//GEN-LAST:event_btnClienteActionPerformed

    private void btnFornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnFornecedorActionPerformed
        showMenu(popupFornecedor);
    }//GEN-LAST:event_btnFornecedorActionPerformed

    private void btnVendaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVendaActionPerformed
        showMenu(popupVenda);
    }//GEN-LAST:event_btnVendaActionPerformed

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        sair();
    }//GEN-LAST:event_btnSairActionPerformed

    private void jMenu3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu3MouseClicked
        logoff();
    }//GEN-LAST:event_jMenu3MouseClicked

    private void jMenu4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu4MouseClicked
        sair();
    }//GEN-LAST:event_jMenu4MouseClicked

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        sair();
    }//GEN-LAST:event_formWindowClosing

    private void btnCompraActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCompraActionPerformed
        showMenu(popupCompra);
    }//GEN-LAST:event_btnCompraActionPerformed

    private void btnColheitaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnColheitaActionPerformed
        showMenu(popupColheita);
    }//GEN-LAST:event_btnColheitaActionPerformed

    private void btnUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUsuarioActionPerformed
        showMenu(popupUsuario);
    }//GEN-LAST:event_btnUsuarioActionPerformed

    private void jMenu2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenu2MouseClicked
        DialogSobre.getInstance().setVisible(true);
    }//GEN-LAST:event_jMenu2MouseClicked

    private void menuClienteGerenciarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuClienteGerenciarActionPerformed
        ViewManageCliente.getInstance().setVisible(true);
    }//GEN-LAST:event_menuClienteGerenciarActionPerformed

    private void menuClienteNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuClienteNovoActionPerformed
        CadastroCliente.getInstance().setVisible(true);
    }//GEN-LAST:event_menuClienteNovoActionPerformed

    private void menuFornecedorNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFornecedorNovoActionPerformed
        CadastroFornecedor.getInstance().setVisible(true);
    }//GEN-LAST:event_menuFornecedorNovoActionPerformed

    private void menuFuncionarioNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFuncionarioNovoActionPerformed
        CadastroFuncionario.getInstance().setVisible(true);
    }//GEN-LAST:event_menuFuncionarioNovoActionPerformed

    private void menuVendaNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuVendaNovoActionPerformed
        CadastroVenda.getInstance().setVisible(true);
    }//GEN-LAST:event_menuVendaNovoActionPerformed

    private void menuCompraNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuCompraNovoActionPerformed
        CadastroCompra.getInstance().setVisible(true);
    }//GEN-LAST:event_menuCompraNovoActionPerformed

    private void menuColheitaNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuColheitaNovoActionPerformed
        CadastroColheita.getInstance().setVisible(true);
    }//GEN-LAST:event_menuColheitaNovoActionPerformed

    private void menuUsuarioNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuUsuarioNovoActionPerformed
        CadastroUsuario.getInstance().setVisible(true);
    }//GEN-LAST:event_menuUsuarioNovoActionPerformed

    private void logoff() {
        setVisible(false);
        UserSession.getInstance().endSession();
        FrameLogin.getInstance().setVisible(true);
    }
    
    private void sair() {        
        String[] options = {"Sair", "Efetuar Logoff", "Cancelar"};
        int result = JOptionPane.showOptionDialog(null, "Deseja encerrar o sistema?", "Sair", 0, JOptionPane.QUESTION_MESSAGE, null, options, "Cancelar");    
        switch (result) {
            case 0: {
                System.exit(0);
                break;
            }
            case 1: {
                logoff();
                break;
            }
        }
    }
    
    private void showMenu(JPopupMenu pPopupMenu) {
        final int mouseX = MouseInfo.getPointerInfo().getLocation().x;
        final int mouseY = MouseInfo.getPointerInfo().getLocation().y;
        pPopupMenu.show(this, mouseX - getLocation().x, mouseY - getLocation().y);
    }
    
    /**
     * @param args the command line arguments
     */
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCliente;
    private javax.swing.JButton btnColheita;
    private javax.swing.JButton btnCompra;
    private javax.swing.JButton btnFornecedor;
    private javax.swing.JButton btnFuncionario;
    private javax.swing.JButton btnRelatorio;
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnUsuario;
    private javax.swing.JButton btnVenda;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem menuClienteGerenciar;
    private javax.swing.JMenuItem menuClienteNovo;
    private javax.swing.JMenuItem menuColheitaGerenciar;
    private javax.swing.JMenuItem menuColheitaNovo;
    private javax.swing.JMenuItem menuCompraGerenciar;
    private javax.swing.JMenuItem menuCompraNovo;
    private javax.swing.JMenuItem menuFornecedorGerenciar;
    private javax.swing.JMenuItem menuFornecedorNovo;
    private javax.swing.JMenuItem menuFuncionarioGerenciar;
    private javax.swing.JMenuItem menuFuncionarioNovo;
    private javax.swing.JMenuItem menuUsuarioGerenciar;
    private javax.swing.JMenuItem menuUsuarioNovo;
    private javax.swing.JMenuItem menuVendaGerenciar;
    private javax.swing.JMenuItem menuVendaNovo;
    private javax.swing.JPopupMenu popupCliente;
    private javax.swing.JPopupMenu popupColheita;
    private javax.swing.JPopupMenu popupCompra;
    private javax.swing.JPopupMenu popupFornecedor;
    private javax.swing.JPopupMenu popupFuncionario;
    private javax.swing.JPopupMenu popupUsuario;
    private javax.swing.JPopupMenu popupVenda;
    // End of variables declaration//GEN-END:variables
}
